config = require('../config')
Schema = config.Schema
mongoose = config.mongoose
util = require("util")

#业务办理的schema
BusiHandlerSchema = new Schema({
  id :
    type : String
    required : true
  image_file_id : String
  name :
    type : String
    required : true
  is_folded :
    type : Boolean
    required : true
  selling_desc :
    order : Number
    content : String
  service_explain :
    order : Number
    content : String
  warming_tip :
    order : Number
    content : String
  frequently_quest :
    order : Number
    content : String
  handler_way :
    order : Number
    content : String
    abbr_way : String
  agreements : [{title:String,content:String}]
  create_date : Date
  updated_at : Date
})


BusiHandlerSchema.methods.add = (req,cb) ->
  this.id = req.param('id')
  this.image_file_id = req.param('image_file_id')
  this.name = req.param('name')
  this.is_folded = req.param('is_folded')
  this.selling_desc = {
    order : req.param('selling_desc.order')
    content : req.param('selling_desc.content')
  }
  this.service_explain = {
    order : req.param('service_explain.order')
    content : req.param('service_explain.content')
  }
  this.warming_tip = {
    order : req.param('warming_tip.order')
    content : req.param('warming_tip.content')
  }
  this.frequently_quest = {
    order : req.param('frequently_quest.order')
    content : req.param('frequently_quest.content')
  }
  this.handler_way = {
    order : req.param('handler_way.order')
    content : req.param('handler_way.content')
    abbr_way : req.param('handler_way.abbr_way')
  }
  this.agreements = []
  this.create_date = new Date
  this.updated_at = new Date
  if util.isArray req.param('agreement.title')
    i=0
    while i<req.param('agreement.title').length
      if(req.param('agreement.title')[i] && req.param('agreement.content')[i])
        agreement = {}
        agreement.title = req.param('agreement.title')[i]
        agreement.content = req.param('agreement.content')[i]
        this.agreements.push agreement
      i++
  else
    if(req.param('agreement.title') && req.param('agreement.content'))
      agreement = {}
      agreement.title = req.param('agreement.title')
      agreement.content = req.param('agreement.content')
      this.agreements.push agreement
  this.save cb

exports.BusiHandler = mongoose.model 'BusiHandler', BusiHandlerSchema